﻿using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto.RKGLDto;
using TR.Domain.Entity;
using TR.Domain.Entity.JH;
using TR.Domain.Entity.RK;
using TR.Domain.Repository.RKGLRepository;

namespace TR.EFCore.Repository.RKDHRePository
{
    public class Wh_Inbill_hRepository : IWh_Inbill_hRepository
    {
        private readonly MyContext context;

        public Wh_Inbill_hRepository(MyContext context)
        {
            this.context = context;
        }
        private int fyzts;
        private int zbzts;
        private int mxzts;
        //关联单号查询
        public async Task<List<GuanlianJHDH>> GLDHFenYeSelectAsync(int mys, int dqy, string? dqsj, string? jssj, GLDHTjDto tj)
        {
            var query = context.GuanlianJHDH.AsQueryable();

            query = query.Where(p => p.sure_state == '4');

            //根据时间查询
            if (!string.IsNullOrEmpty(dqsj) && !string.IsNullOrEmpty(jssj))
            {
                query = query.Where(p => string.Compare(p.buy_date, dqsj) >= 0 && string.Compare(p.buy_date, jssj) <= 0);
            }

            //根据实体类条件查询
            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.Buy_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.buy_code, "%" + tj.Buy_code + "%"));
                }
               if (tj.Sure_state != '\0')
                {
                    query = query.Where(p => p.sure_state == tj.Sure_state);
                }
                if (!string.IsNullOrEmpty(tj.Supply_code))
                {
                    query = query.Where(p => p.supply_code == tj.Supply_code);
                }
                if (!string.IsNullOrEmpty(tj.Buy_man))
                {
                    query = query.Where(p => p.buy_man == tj.Buy_man);
                }
            }
            var result = await query.OrderByDescending(p => p.buy_date).Skip(dqy * (mys - 1)).Take(dqy).ToListAsync();//异步使用ToListAsync

            fyzts = await query.CountAsync();

            return result;
        }
        //总条数
        public async Task<int> Ztshu()
        {
            return fyzts;
        }

        //入库信息添加
        public async Task<bool> widInsert(wh_Inbill_h wih, List<wh_Inbill_d> wid)
        {
            var query = context;
            try
            {
                var wihadd = new wh_Inbill_h()
                {
                    inbill_code = wih.inbill_code,
                    ware_code = wih.ware_code,
                    inbill_type = wih.inbill_type,
                    inbill_state = wih.inbill_state,
                    inbll_date = wih.inbll_date,
                    buy_code = wih.buy_code,
                    supply_code = wih.supply_code,
                    link_man = wih.link_man,
                    link_way = wih.link_way,
                    inbill_duty_man = wih.inbill_duty_man,
                    remark = wih.remark,
                    create_by = wih.create_by,
                    create_time = wih.create_time,
                    sure_date = wih.sure_date,
                    driver_name = wih.driver_name,
                    car_id = wih.car_id,
                    is_del = 0,
                };
                query.wh_Inbill_h.Add( wihadd );

                foreach (var xq in wid)
                {
                    var wids = new wh_Inbill_d()
                    {
                        item_code = xq.item_code,
                        inbill_code = wih.inbill_code,
                        item_type = xq.item_type,
                        unit = xq.unit,
                        buy_num = xq.buy_num,
                        inbill_num = xq.inbill_num,
                        area_code = xq.area_code,
                        buy_price = xq.buy_price,
                        product_date = xq.product_date,
                        batch_no = xq.batch_no,
                        create_by = xq.create_by,
                        create_time = xq.create_time,
                        is_del= 0,
                    };
                    query.wh_Inbill_d.Add( wids );
                }
                await query.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息:" + ex.Message);
                return false;
            }
        }

        //根据进货单号查询对应货品
        public async Task<List<JHDHchaHP>> JHDHchahp(string buy_code, HPTj tj)
        {
            var query = context.JHDHchaHP.AsQueryable();

            query = query.Where(p => p.buy_code == buy_code);

            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.item_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.item_code, "%" + tj.item_code + "%"));
                }
                if (!string.IsNullOrEmpty(tj.unit))
                {
                    query = query.Where(p => p.unit == tj.unit);
                }
            }
            var result = query.ToListAsync();

            return await result;
        }

        //仓库下拉框查询
        public Task<List<bs_WareHouse>> CKXLKSelect()
        {
            var query = context.bs_warehouse.AsQueryable();

            query.Where(p => p.is_del == 0);

            return query.ToListAsync();
        }

        //入库管理主表查询
        public async Task<List<RKGL_SelectZB>> RKGL_SelectZB(int mys, int dqy, RKGLZbTjCx tj)
        {
            var query = context.RKGL_SelectZB.AsQueryable();

            query = query.Where(p => p.is_del == 0);

            //根据实体类条件查询
            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.inbill_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.inbill_code, "%" + tj.inbill_code + "%"));
                }
                if (!string.IsNullOrEmpty(tj.inbill_type))
                {
                    query = query.Where(p => p.inbill_type == tj.inbill_type);
                }
                if (tj.sure_state != '\0')
                {
                    query = query.Where(p => p.sure_state == tj.sure_state);
                }
                if (!string.IsNullOrEmpty(tj.ware_name))
                {
                    query = query.Where(p => p.ware_name == tj.ware_name);
                }
            }
            var result = await query.OrderByDescending(p => p.create_time).Skip(mys * (dqy - 1)).Take(mys).ToListAsync();//异步使用ToListAsync

            zbzts = await query.CountAsync();

            return result;

        }
        //入库主表总条数
        public async Task<int> RKZtShu()
        {
            return zbzts;
        }
        //批量改变进货状态
        public async Task<bool> UpdateState(List<int> Bian, char zt)
        {
            try
            {
                var query = context;

                var staticUpdate = query.wh_Buy_Order_H.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in staticUpdate)
                {
                    id.sure_state = zt;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //根据仓库编号查询库位
        public async Task<List<bs_Ware_Area>> SelectWare(string ware_code)
        {
            var query = context;

            return await query.bs_Ware_Area.Where(p => p.ware_code == ware_code).ToListAsync();
        }
        //批量逻辑删除
        public async Task<bool> RKGLDelect(List<int> isdel)
        {
            try
            {
                var query = context;

                var rkglDel = query.wh_Inbill_h.Where(p => isdel.Contains(p.id)).ToList();

                foreach (var id in rkglDel)
                {
                    id.is_del = 1;
                    query.Entry(id).State= EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误"); 
                return false;
            }
        }

        //单条逻辑删除
        public async Task<bool> RKGLDel(int Bian)
        {
            try
            {
                var query = context;

                var rkdel = query.wh_Inbill_h.Where(p => p.id == Bian);

                foreach (var id in rkdel)
                {
                    id.is_del = 1;
                    query.Entry(id).State = EntityState.Modified;
                }
                await query.SaveChangesAsync();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //入库管理批量审核
        public async Task<bool> SHUpdate(List<int> Bian, char state, string sureman, string suredate, string remark)
        {
            try
            {
                var query = context;

                var ShenHeUpdate = query.wh_Inbill_h.Where(p => Bian.Contains(p.id)).ToList();

                foreach (var id in ShenHeUpdate)
                {
                    id.sure_state = state;
                    id.sure_man = sureman;
                    id.sure_date = suredate;
                    id.remark = remark;
                    query.Entry(id).State= EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //入库管理批量撤销
        public async Task<bool> CXUpdate(List<int> Bian)
        {
            try
            {
                var query = context;

                var CheXiao = query.wh_Inbill_h.Where(p => Bian.Contains(p.id)).ToList();

                foreach (var id in CheXiao)
                {
                    id.sure_state = '2';
                    id.sure_man = "";
                    id.sure_date = "";
                    id.remark = "";
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //根据编号查询信息
        public async Task<wh_Inbill_h> RKBhSelect(string bh)
        {
            var query = await context.wh_Inbill_h.Where(p => p.inbill_code == bh).FirstOrDefaultAsync();
            return query;
        }

        //编辑入库明细查询
        public async Task<List<RKMXXG>> RKMXXGSelect(int mys, int dqy, string tj)
        {
            var query = context.RKMXXG.AsQueryable();

            query = query.Where(p => p.inbill_code == tj);

            var result = await query.OrderByDescending(p => p.product_date).Skip(mys * (dqy - 1)).Take(mys).ToListAsync();//异步使用ToListAsync

            mxzts = await query.CountAsync();

            return result;
        }

        //明细总条数
        public async Task<int> MXCount()
        {
            return mxzts;
        }

        //入库信息修改
        public async Task<bool> RKUpdate(wh_Inbill_h jcxx, List<wh_Inbill_d> rkmx)
        {
            try
            {
                var query = context;
                var wbohUpdate = query.wh_Inbill_h.FirstOrDefault(p => p.inbill_code == jcxx.inbill_code);
                if (wbohUpdate != null)
                {
                    wbohUpdate.ware_code = jcxx.ware_code;
                    wbohUpdate.inbill_type = jcxx.inbill_type;
                    wbohUpdate.inbll_date = jcxx.inbll_date;
                    wbohUpdate.buy_code = jcxx.buy_code;
                    wbohUpdate.supply_code = jcxx.supply_code;
                    wbohUpdate.link_man = jcxx.link_man;
                    wbohUpdate.link_way = jcxx.link_way;
                    wbohUpdate.inbill_duty_man = jcxx.inbill_duty_man;  
                    wbohUpdate.remark = jcxx.remark;
                    wbohUpdate.update_time = jcxx.update_time;
                    wbohUpdate.update_by = jcxx.update_by;
                    wbohUpdate.sure_state = '2';

                    //var wbohdel = query.wh_orderbuy_d.Where(p => p.buy_code == jhzb.buy_code).ToList();
                    //query.wh_orderbuy_d.RemoveRange(wbohdel);

                    foreach (var mx in rkmx)
                    {
                        var wod = new wh_Inbill_d()
                        {
                            inbill_code = jcxx.inbill_code,
                            inbill_num = mx.inbill_num,
                            area_code = mx.area_code,
                            batch_no = mx.batch_no,
                            product_date = mx.product_date,
                            update_time = mx.update_time,
                            update_by = mx.update_by,
                        };
                        query.wh_Inbill_h.Update(wbohUpdate);
                        query.wh_Inbill_d.Add(wod);
                    }
                    await query.SaveChangesAsync();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息" + ex.Message);
                return false;
            }

        }
    }
}
